package org.dtrd.modules.content.articles.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.CharUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dtrd.modules.content.articles.config.ArticleConfig.ArticleCategoryConfig;
import org.dtrd.modules.content.articles.entity.bean.ArticleInfo;
import org.dtrd.modules.content.articles.entity.bean.GroupItem;
import org.dtrd.modules.content.articles.entity.bean.Vocabulary;
import org.dtrd.modules.content.articles.entity.po.DtrdExtEmsContent;
import org.dtrd.modules.content.articles.mapper.DtrdExtEmsContentMapper;
import org.dtrd.modules.content.articles.service.IDtrdExtEmsCategoryService;
import org.dtrd.modules.content.articles.service.IDtrdExtEmsContentArticleService;
import org.dtrd.modules.content.articles.service.IDtrdExtEmsContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 萧
 * @date 2021-08-25 17:37:46
 **/
@Service
@DS("multi-datasource1")
@Slf4j
public class DtrdExtEmsContentServiceImpl extends ServiceImpl<DtrdExtEmsContentMapper, DtrdExtEmsContent> implements IDtrdExtEmsContentService {

    @Autowired
    private IDtrdExtEmsContentArticleService contentArticleService;

    @Autowired
    private IDtrdExtEmsCategoryService categoryService;

    @Override
    public IPage<DtrdExtEmsContent> getArticleList(Integer pageNum, Integer pageSize, Integer articleCategory, String title, String keyword) {
        // 处理分页参数
        Page<DtrdExtEmsContent> page = new Page<>(pageNum, pageSize);
        //拼装查询条件
        LambdaQueryWrapper<DtrdExtEmsContent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DtrdExtEmsContent::getIsDel, 0);
        if (articleCategory != null) {
            lambdaQueryWrapper.eq(DtrdExtEmsContent::getClassId, articleCategory);
        }
        if (title != null) {
            lambdaQueryWrapper.like(DtrdExtEmsContent::getTitle, title);
        }
        if (keyword != null) {
            String[] split = keyword.split(",");
            for (String key : split) {
                lambdaQueryWrapper.like(DtrdExtEmsContent::getKeywords, key);
            }
        }
        lambdaQueryWrapper.orderByDesc(DtrdExtEmsContent::getContentId);
        // 执行查询
        page = page(page, lambdaQueryWrapper);
        return page;
    }

    @Override
    public ArticleInfo getArticleInfoById(Integer dataId) {
        ArticleInfo articleInfo = baseMapper.selectArticleInfoByContentId(dataId);
        articleInfo.setClassName(categoryService.getCategoryNameById(articleInfo.getClassId()));
        return articleInfo;
    }

    @Override
    public void clearArticleClass(Integer classId) {
        baseMapper.clearArticleClass(classId);
    }

    @Override
    public void manageArticle(ArticleInfo info) {
        DtrdExtEmsContent content = new DtrdExtEmsContent();
        if (info.getContentId() == null) {
            content.init();
        }
        content.setContentId(info.getContentId());
        content.setClassId(info.getClassId());
        content.setTitle(info.getTitle());
        content.setUrl(info.getUrl());
        content.setKeywords(info.getKeywords());
        content.setDescription(info.getDescription());
        content.setImage(info.getImage());
        content.setSequence(info.getSequence());
        content.setStatus(info.getStatus());
        content.setViews(info.getViews());
        content.setView(info.getCustomViews());
        content.setTopMark(info.getTopMark());
        content.setAudio(info.getAudio());
        content.setVideo(info.getVideo());
        content.setCourseType(info.getCourseType());
        saveOrUpdate(content);
        if (StringUtils.isNotEmpty(info.getContent())) {
            // 保存文章内容
            contentArticleService.saveArticleInfo(content.getContentId(), info.getContent());
        }
    }

    @Override
    public void deleteArticle(Integer articleId) {
        DtrdExtEmsContent content = baseMapper.selectById(articleId);
        if (content != null) {
            content.setIsDel(1);
            saveOrUpdate(content);
        }
    }

    @Override
    public List<GroupItem<String, ArticleInfo>> getGroupRecommendArticleList() {
        // 每组下前三条 肖 2022-08-02 16:51:06
        List<ArticleInfo> list = baseMapper.getRecommendArticleList(ArticleCategoryConfig.PATIENT.getCategory(), 3);
        if (CollectionUtil.isNotEmpty(list)) {
            List<GroupItem<String, ArticleInfo>> dataList = new ArrayList<>(list.size() / 3);
            Map<String, List<ArticleInfo>> map = new HashMap<>(list.size() / 3);
            list.forEach(vo -> {
                List<ArticleInfo> tmpList = map.getOrDefault(vo.getClassName(), new ArrayList<>());
                tmpList.add(vo);
                map.put(vo.getClassName(), tmpList);
            });
            map.keySet().stream().map(key -> new GroupItem<>(key, map.get(key)))
                    .forEach(dataList::add);
            return dataList;
        }
        return null;
    }

}
